/*
* Copyright (C) 2004-2006  Autodesk, Inc.
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of version 2.1 of the GNU Lesser
* General Public License as published by the Free Software Foundation.
* 
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
*/

#include "stdafx.h"
#include "FdoWcsGridCRS.h"
#include "FdoWcsXmlGlobals.h"

const FdoStringP gDefaultGridType = L"urn:ogc:def:method:WCS:1.1:2dSimpleGrid";
const FdoStringP gDefaultGridCS = L"urn:ogc:def:cs:OGC:0.0:Grid2dSquareCS";

FdoWcsGridCRS::FdoWcsGridCRS(void)
{
    double defaultPosition[2] = {0, 0};
    mGridOrigin = FdoDoubleArray::Create(defaultPosition, 2);
    mGridOffsets = FdoDoubleArray::Create(defaultPosition, 2);

    mGridType = gDefaultGridType;
    mGridCS = gDefaultGridCS;
}

FdoWcsGridCRS::~FdoWcsGridCRS(void)
{
    FDO_SAFE_RELEASE(mGridOrigin);
    FDO_SAFE_RELEASE(mGridOffsets);
}

FdoWcsGridCRS* FdoWcsGridCRS::Create()
{
    return new FdoWcsGridCRS();
}

void FdoWcsGridCRS::Dispose()
{
    delete this;
}

void FdoWcsGridCRS::InitFromXml(FdoXmlSaxContext* context, FdoXmlAttributeCollection* attrs)
{
    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(attrs);
        
        FdoXmlAttributeP att = attrs->FindItem(FdoWcsXmlGlobals::WcsGridCRSAttributeId);
        if (att != NULL) {
            mId = att->GetValue();
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }
}

FdoXmlSaxHandler* FdoWcsGridCRS::XmlStartElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname, FdoXmlAttributeCollection *atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try
    {
        VALIDATE_ARGUMENT(context);
        VALIDATE_ARGUMENT(name);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL)
        {
            mXmlContentHandler = FdoXmlCharDataHandler::Create();
            pRet = mXmlContentHandler;
        }
    }
    catch (FdoException* e)
    {
        context->AddError(e);
        e->Release();
    }

    return pRet;
}

FdoBoolean FdoWcsGridCRS::XmlEndElement(FdoXmlSaxContext *context, FdoString *uri, FdoString *name, FdoString *qname)
{
    try 
    {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSSRSName) == 0)
        {
            mSRSName = mXmlContentHandler->GetString();
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSGridBaseCRS) == 0) 
        {
            SetGridBaseCRS(mXmlContentHandler->GetString());
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSGridType) == 0) 
        {
            SetGridType(mXmlContentHandler->GetString());
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSGridOrigin) == 0) 
        {
            FdoStringsP positions = FdoStringCollection::Create(mXmlContentHandler->GetString(), FdoWcsXmlGlobals::WcsGridCRSPositionDelimiter);
            double numbers[2];
            numbers[0] = FdoCommonOSUtil::wtof(positions->GetString(0));
            numbers[1] = FdoCommonOSUtil::wtof(positions->GetString(1));
            mGridOrigin = FdoDoubleArray::Append(mGridOrigin, 2, numbers);
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSGridOffsets) == 0)
        {
            FdoStringsP positions = FdoStringCollection::Create(mXmlContentHandler->GetString(), FdoWcsXmlGlobals::WcsGridCRSPositionDelimiter);
            double numbers[2];
            numbers[0] = FdoCommonOSUtil::wtof(positions->GetString(0));
            numbers[1] = FdoCommonOSUtil::wtof(positions->GetString(1));
            mGridOffsets = FdoDoubleArray::Append(mGridOffsets, 2, numbers);
        }
        else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::WcsGridCRSGridCS) == 0) 
        {
            SetGridCS(mXmlContentHandler->GetString());
        }

        FDO_SAFE_RELEASE(mXmlContentHandler.p);
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return BaseType::XmlEndElement(context, uri, name, qname);
}

FdoString* FdoWcsGridCRS::GetSRSName() const
{
    return mSRSName;
}

FdoString* FdoWcsGridCRS::GetGridBaseCRS() const
{
    return mGridBaseCRS;
}

void FdoWcsGridCRS::SetGridBaseCRS(FdoString* value)
{
    mGridBaseCRS = value;
}

FdoString* FdoWcsGridCRS::GetGridType() const
{
    return mGridType;
}

void FdoWcsGridCRS::SetGridType(FdoString* value)
{
    mGridType = value;
}

FdoDoubleArray* FdoWcsGridCRS::GetGridOrigin() const
{
    return mGridOrigin;
}

void FdoWcsGridCRS::SetGridOrigin(FdoDoubleArray* value)
{
    mGridOrigin = value;
}

FdoDoubleArray* FdoWcsGridCRS::GetGridOffsets() const
{
    return mGridOffsets;
}

void FdoWcsGridCRS::SetGridOffsets(FdoDoubleArray* value)
{
    mGridOffsets = value;
}

FdoString* FdoWcsGridCRS::GetGridCS() const
{
    return mGridCS;
}

void FdoWcsGridCRS::SetGridCS(FdoString* value)
{
    mGridCS = value;
}

FdoString* FdoWcsGridCRS::GetId() const
{
    return mId;
}